import {listDictCodes} from "@/api/sys/dictionaryApi";
import {baseUrl} from "@/config/env";

const globalFormVariable = {
    "#{baseUrl}": baseUrl
}

function getAvueColType(col) {
    return col.component || col.type
}

function handlerOptionVariable(avueOption) {
    if (!avueOption) {
        return
    }
    let column = avueOption.column || []
    if (!column) {
        return;
    }
    for (const col of column) {
        handlerColBaseUrl(col)
    }
    let group = avueOption.group
    if (!group) {
        return;
    }
    for (const groupItem of group) {
        handlerOptionVariable(groupItem.children)
    }
}

function convertVariable(srcStr) {
    for (let key of globalFormVariable) {
        srcStr = srcStr.replace(key, globalFormVariable[key]);
    }
    return srcStr
}

function handlerColBaseUrl(col) {
    let colType = getAvueColType(col)
    if ('dynamic' === colType) {
        handlerOptionVariable(col.children)
        return
    }
    if (!globalFormVariable) return;
    if (col.dicUrl) {
        col.dicUrl = convertVariable(col.dicUrl)

    } else if (col.action) {
        col.action = convertVariable(col.action)
    }

}


const avueOptionTools = {
    upload: {
        action: "#{baseUrl}/start/oss/resource/formUpload",
        props: {
            label: "ossName",
        },
        propsHttp: {
            name: 'ossName',
            res: 'data',
            url: 'ossUrl',
        },

    },
    dictUrl: "#{baseUrl}/start/sysDictionaryItem/listByDictionaryCode",
    dicMethod: 'get',
    paramsKey: "dictCode",
    dicProps: {
        label: "dictName",
        value: "dictCode",
    },
    loadDictCodes: listDictCodes,
}
export default avueOptionTools
